<?php
//error_reporting(E_ALL);

//http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=Main%20Page

include 'text.php';

foreach($array as $startText)
{

	//separate title and author into two
	$splitText = explode("<>",$startText);

	//I shall wear midnight
	//titles with spaces don't work 'the fry chronicles'
	//if multiple results, add 'novel' e.g. brave new world
	//if 'missing' attribute is present, article does not exist

	//starting information
	//$title = 'Hunting of the snark, the';
	$title = $splitText[0];
	$author = $splitText[1];

	//parse determiners from title
	//The
	if (stripos($title, ', The')>=1)
	{
		$title = str_ireplace(', The', '', $title);
		$title = 'The '.$title;
	}
	//An
	if (stripos($title, ', An')>=1)
	{
		$title = str_ireplace(', An', '', $title);
		$title = 'An '.$title;
	}
	//A
	if (stripos($title, ', A')>=1)
	{
		$title = str_ireplace(', A', '', $title);
		$title = 'A '.$title;
	}

	//convert to lower case
	$title = strtolower($title);
	$title = urlencode($title);
	$author = urlencode($author);

	//construct url
	$rooturl = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles';
	$completeurl = "$rooturl=$title&redirects&continue=";

	echo "URL: ".$completeurl."<br/>";

	//construct curl request
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$completeurl);
	//curl_setopt($ch, CURLOPT_HEADER, 1); //remove headers for xml
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
	$request = curl_exec($ch);

	$xml = simplexml_load_string($request);//get file xml

	$xml2 = $xml->query->pages->page->revisions->rev;//get actual article text

	$xml3 = explode('|', $xml2);//split into fields array

	$pubYearA = preg_grep('/pub_date/', $xml3);//find elements in the array that match regex
	$pubYearB = preg_grep('/published/', $xml3);

	if (is_array($pubYearA)&&count($pubYearA)>0)
	{
		$pubYearA = array_values($pubYearA);//reindex
		$pubYear = $pubYearA[0];//should be 1 result
		$pubYear = trim(str_ireplace('pub_date', '', $pubYear));//remove text and trim
		$pubYear = trim(str_ireplace('=', '', $pubYear));//remove text and trim

		if (strlen($pubYear)>4)
		{
			$date = date_create_from_format('d F Y', $pubYear);
			$pubYear = date_format($date,'Y');
		}
	}
	
	else if (is_array($pubYearB)&&count($pubYearB)>0)
	{
		$pubYearB = array_values($pubYearB);//reindex
		print_r($pubYearB);
		$pubYear = $pubYearB[0];//should be 1 result
		$pubYear = trim(str_ireplace('published', '', $pubYear));//remove text and trim
		$pubYear = trim(str_ireplace('=', '', $pubYear));//remove text and trim
		$pubYear = substr($pubYear, 0,4);
		echo "Pubyear text: ".$pubYear;
		if (strlen($pubYear)>4&&strlen($pubYear)<25)
		{
			$date = date_create_from_format('d F Y', $pubYear);
			$pubYear = date_format($date,'Y');
		}
		else if (strlen($pubYear)>=25)
		{
			
			$pubYear='';
		}
		
	}

	echo $splitText[0]," Year: ",$pubYear,"<br/><br/>";
	unset ($pubYearA);
	unset ($pubYearB);
	unset ($pubYear);

}

?>